package com.barneyx.mshinfoplatform.web.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.barneyx.mshinfoplatform.common.ExResult;
import com.barneyx.mshinfoplatform.entity.area.AreaEntity;
import com.barneyx.mshinfoplatform.entity.info.InfoSettingEntity;
import com.barneyx.mshinfoplatform.service.area.IAreaEntityService;
import com.barneyx.mshinfoplatform.service.info.IInfoSettingEntityService;
import com.barneyx.mshinfoplatform.util.AreaUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/admin/area/")
public class AdminAreaSettingController {


    @Resource
    private IAreaEntityService areaEntityService;

    @Resource
    private IInfoSettingEntityService infoSettingEntityService;

    @Resource
    private AreaUtil areaUtil;


    @GetMapping("list")
    public ExResult<?> getList() {
        return ExResult.ofAuto(areaEntityService.list());
    }

    @GetMapping("list-top")
    public ExResult<?> getTopList() {
        return ExResult.ofAuto(areaEntityService.list(new LambdaQueryWrapper<AreaEntity>()
                .eq(AreaEntity::getAreaParentId, "0")));
    }


    @PostMapping("create")
    public ExResult<?> createArea(@RequestBody AreaEntity data,
                                  @RequestParam(value = "child", defaultValue = "false", required = false) boolean child) {
        return ExResult.ofAuto(areaUtil.createArea(data, child));
    }


    @GetMapping("set-default-area")
    public ExResult<?> setDefaultArea(@RequestParam("id") String areaId) {
        return ExResult.ofAuto(areaUtil.setDefaultArea(areaId));
    }


    @PutMapping("update")
    public ExResult<?> updateArea(@RequestBody AreaEntity area) {
        return ExResult.ofAuto(areaUtil.updateArea(area));
    }

    @DeleteMapping("delete-by-id")
    public ExResult<?> deleteById(@RequestParam("id") String areaId) {
        return ExResult.ofAuto(areaUtil.deleteAreaById(areaId));
    }

    @GetMapping("area-by-id")
    public ExResult<?> getById(@RequestParam("id") String areaId) {
        return ExResult.ofAuto(areaUtil.getAreaById(areaId));
    }


    @GetMapping("init-area-config")
    public ExResult<?> initAreaConfig() {
        areaUtil.CheckInfoSetting();
        return ExResult.ofSuccess("ok");
    }


}
